/** \file    qio.cpp
  * \brief   Brief description.
  * \brief   images input/output (QT-specific).
  * \date    2002, 2003
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/imglib/src/qio.cpp,v 1.2 2004/04/30 19:13:30 cscom1r4 Exp $
  */

#include "imglib/io.h"
#include "imglib/convert.h"
#include <qimage.h> //if you have not Qt then make empty file "qimage.h" to prevent errors

#ifdef QIMAGE_H //Qt library realy presents

#include <qfileinfo.h>

#pragma comment(lib, "qt-mt230nc.lib")

namespace img {

bool qt_reader::ping(const std::string & path, file_info * info) const
{
	if (info) {
		QImage qimg;
		if (qimg.load(path.c_str())) {
			if (info) {
				info->path = path;
				info->format = QImageIO::imageFormat(path.c_str()); 
				info->dim = size_extent(qimg.width(), qimg.height());
				info->bit_count = qimg.depth(); //!not exact
				info->compressed = false; //!information is lost
			}
			return true;
		} else
			return false;
	}
	else {
		return QImageIO::imageFormat(path.c_str()) != 0;
	}
}

bool qt_reader::ping(const std::string & path, const std::string & format, file_info * info) const
{
	if (QImageIO::imageFormat(path.c_str()) != 0)
		return true;
	else {
		//perhaps information about format can improve understanding
		QImage qimg;
		if (qimg.load(path.c_str(), format.c_str())) {
			if (info) {
				info->path = path;
				info->format = format; 
				info->dim = size_extent(qimg.width(), qimg.height());
				info->bit_count = qimg.depth(); //!not exact
				info->compressed = false; //!information is lost
			}
			return true;
		} else
			return false;
	}
}

bool qt_reader::fast_ping() const
{
	return true;
}

void qt_reader::read(const std::string & path, byte_image & img) const
{
	QImage qimg;
	if (!qimg.load(path.c_str()))
		throw io_error("could not read image", path);
	qimg = trueColor(qimg);
	byte_image s;
	shareQdata(s, qimg);
	img = s;
}

void qt_reader::read(const std::string & path, const std::string & format, byte_image & img) const
{
	QImage qimg;
	if (!qimg.load(path.c_str(), format.c_str()))
		throw io_error("could not read image", path);
	qimg = trueColor(qimg);
	byte_image s;
	shareQdata(s, qimg);
	img = s;
}

static inline void QStrList2ext_vec(QStrList &l, ext_vec &v)
{
	v.clear();
	for (l.first(); l.current() != 0; l.next())
		v.push_back(l.current());
}

void qt_reader::formats(ext_vec & f) const
{
	QStrList2ext_vec(QImageIO::inputFormats(), f);
}

size_t qt_writer::write(const std::string & path, const std::string & format, const byte_image & img, bool alpha)
{
	QString qfmt = QString(format.c_str()).upper();
	if (QImageIO::outputFormats().find(qfmt) == -1)
		throw io_error("unsupported output format", path);

	QImage qimg = img::qimg(img);
	qimg.setAlphaBuffer(alpha);
	if (!qimg.save(path.c_str(), qfmt))
		throw io_error("could not write image", path.c_str());

	return QFileInfo(path.c_str()).size();
}

void qt_writer::formats(ext_vec & f) const
{
	QStrList2ext_vec(QImageIO::outputFormats(), f);
}

our_qt_im_multireader::our_qt_im_multireader()
{
	insert(new our_reader());
	insert(new qt_reader());
	insert(new magick_reader());
}

our_qt_im_multiwriter::our_qt_im_multiwriter()
{
	insert(new our_writer());
	insert(new qt_writer());
	insert(new magick_writer());
}

} //namespace img

#endif //QIMAGE_H
